package me.demo;

import me.arraylist.Test.StudentTest;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class App {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选则操作：1登录 2注册 3忘记密码 4退出");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                case "4" -> {
                    System.out.println("谢谢使用");
                    break loop;
                }
                default -> System.out.println("输入错误");
            }
        }
    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contains(list, username);
        if (!flag) {
            System.out.println("用户名不存在,请先注册");
            return;
        }
        System.out.println("请输入身份证号码");
        String passID = sc.next();
        System.out.println("请输入手机号");
        String phoneNumber = sc.next();
        int index = findIndex(list, username);
        User us = list.get(index);
        if (!(us.getID().equalsIgnoreCase(passID) && us.getPhone().equals(phoneNumber))) {
            System.out.println("身份证号码或手机号错误");
            return;
        }
        String password;
        while (true) {
            System.out.println("请输入新的密码");
            password = sc.next();
            System.out.println("请再次输入新的密码");
            String againPassword = sc.next();
            if (againPassword.equals(password)) {
                System.out.println("两次密码一致");
                break;
            } else {
                System.out.println("两次密码不一致,请重新输入");
                continue;
            }
        }
        us.setPassword(password);
        System.out.println("密码修改成功");
    }

    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User us = list.get(i);
            if (us.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String username = sc.next();
            boolean flag = contains(list, username);
            if (!flag) {
                System.out.println("用户名不存在");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.next();
            while (true) {
                String rightCode = getCode();
                System.out.println("正确的验证码为:" + rightCode);
                System.out.println("请输入验证码");
                String code = sc.next();
                if (code.equalsIgnoreCase(rightCode)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误");
                    continue;
                }
            }
            User useInfo = new User(username, password, null, null);
            boolean result = checkUserInfo(list, useInfo);
            if (result) {
                System.out.println("登录成功");
                StudentTest st = new StudentTest();
                st.startStudentTest();
                break;
            } else {
                System.out.println("登录失败");
                if (i == 2) {
                    System.out.println("3次登录失败,退出");
                    return;
                } else {
                    System.out.println("用户名或密码错误，还剩下" + (2 - i) + "次机会");
                }
            }
        }
    }


    private static boolean checkUserInfo(ArrayList<User> list, User useInfo) {
        for (int i = 0; i < list.size(); i++) {
            User us = list.get(i);
            if (us.getUsername().equals(useInfo.getUsername()) && us.getPassword().equals(useInfo.getPassword())) {
                System.out.println("登录成功");
                return true;
            }
        }
        System.out.println("登录失败");
        return false;
    }

    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        String password;
        String passID;
        String phoneNumber;
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();
            boolean flag1 = checkUsername(username);
            if (!flag1) {
                System.out.println("用户名格式不符合要求");
                continue;
            }
            boolean flag2 = contains(list, username);
            if (flag2) {
                System.out.println("用户名已存在");
            } else {
                System.out.println("用户名可用");
                break;
            }
        }
        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("请再次输入密码");
            String againPassword = sc.next();
            if (!againPassword.equals(password)) {
                System.out.println("两次密码不一致,请重新输入");
                continue;
            } else {
                System.out.println("密码可用");
                break;
            }
        }
        while (true) {
            System.out.println("请输入身份证号码");
            passID = sc.next();
            boolean flag = checkID(passID);
            if (flag) {
                System.out.println("身份证号码可用");
                break;
            } else {
                System.out.println("身份证号码格式不符合要求,请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入手机号码");
            phoneNumber = sc.next();
            boolean flag = checkPhoneNumber(phoneNumber);
            if (flag) {
                System.out.println("手机号可用");
                break;
            } else {
                System.out.println("手机号格式不符合要求,请重新输入");
                continue;
            }
        }
        User us = new User(username, password, passID, phoneNumber);
        list.add(us);
        System.out.println("注册成功");
        printList(list);
    }

    private static void printList(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User us = list.get(i);
            System.out.println(us.getUsername() + "," + us.getPassword() + "," + us.getID() + "," + us.getPhone());
        }
    }

    private static boolean checkPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            return false;
        }
        if (phoneNumber.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }

    private static boolean checkID(String passID) {
        if (passID.length() != 18) {
            return false;
        }
        if (passID.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < passID.length() - 1; i++) {
            char c = passID.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        char c1 = passID.charAt(17);
        if ((c1 >= '0' && c1 <= '9') || c1 == 'X' || c1 == 'x') {
            return true;
        } else {
            return false;
        }
    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User us = list.get(i);
            if (us.getUsername().equals(username)) {
                System.out.println("用户名已存在");
                return true;
            }
        }
        return false;
    }

    private static boolean checkUsername(String username) {
        if (username.length() < 3 || username.length() > 10) {
            return false;
        }
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
                count++;
                break;
            }
        }
        return count > 0;
    }

    private static String getCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) (i + 'a'));
            list.add((char) (i + 'A'));
        }
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        int index = r.nextInt(10);
        sb.append(index);
        char[] arr = sb.toString().toCharArray();
        int index1 = r.nextInt(arr.length);
        int index2 = r.nextInt(arr.length);
        char temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
        return new String(arr);
    }
}
